$(function() {
	$("#create_domain_form").validationEngine({promptPosition: "bottomRight:35,-33"}); 
	$("#open1").click(function(){
		 $("#hot_rx").show();
	})
	$("#close1").click(function(){
		$("#hot_rx").hide();
	})
	$("#open2").click(function(){
		$("#zhanqu1").show();
	})
	$("#close2").click(function(){
		$("#zhanqu1").hide();
	})
	$("#open3").click(function(){
		$("#zhanqu2").show();
	})
	$("#close3").click(function(){
		$("#zhanqu2").hide();
	})
	var browserTabImg =$("#tabImg").val();
	 var x = browserTabImg;
	 var idx = x.lastIndexOf("/");
	 if (idx > 0){
		 $("#file_name_e").val(x.substring(idx+20));
		 $("#Imgicon").attr("src",browserTabImg);
	 } 
	 var browserTabTitle= $("#tabTital").val();
	 if(browserTabTitle){
		 $("#arrtitle").text(browserTabTitle);
		 $("#title_rx").val(browserTabTitle);
	 }
	 
	 var logo =$("#hidlogo").val();
	 var x = logo;
	 var idx = x.lastIndexOf("/");
	 if (idx > 0){
		 $("#file_name_a").val(x.substring(idx+20));
	 } 
	 
	 var bigImg =$("#hidBigImg").val();
	 var x = bigImg;
	 var idx = x.lastIndexOf("/");
	 if (idx > 0){
		 $("#file_name_b").val(x.substring(idx+20));
	 } 
	 
	 //加载页面初始化赋值
	  
	 
	 var hotline = $("#hotline").val();
	 if(hotline&& hotline!='{}'){
		 var json0 =  jQuery.parseJSON(hotline);
		 if(json0.hoteline){
			 $("#line").text(json0.hoteline);
			 $("#hot_rx").val(json0.hoteline);
		 }else{
			 $(".zhy0629_iphone").hide();
		 }
		 if(json0.buttonState){
			 //1;//按钮为开启状态
			 if(json0.buttonState=='0'){
			 	$("#open1").attr("class",'kq kq1');
			 	$("#close1").attr("class",'gb gb1');
			 	$("#hot_rx").hide();
			 }else{
				 $("#hot_rx").show();
				 $(".zhy0629_iphone").show();
			 } 
		 }
	 }else{
		 $(".zhy0629_iphone").show();
	 }
	 
	 var buttonColor = $("#buttonColor").val();
	 if(buttonColor&& buttonColor!='{}'){
		 var json0 =  jQuery.parseJSON(buttonColor);
		 if(json0.login){
			 $("#login").attr("class",json0.login);
 			 $(".dl_a").attr("class",json0.login);
		 }
		 if(json0.register){
			 $("#register").attr("class",json0.register);
			 $(".zc_a").attr("class",json0.register);
		 }
	 }
	 
	 var displayAreaOne = $("#displayAreaOne").val();
	 if(displayAreaOne &&displayAreaOne!='{}'){
		 var json1 = jQuery.parseJSON(displayAreaOne);
		 if(json1.chinese){
			 $("#name1").text(json1.chinese);
			 $("#c_wz_a").val(json1.chinese);
		 }
		 if(json1.english){
			 $(".dda").text(json1.english);
			 $("#c_wz_b").val(json1.english);
		 }
		 if(json1.img){
			 $("#ImgPr_c").attr("src",json1.img);
			 $("#show1_img").val(json1.img);
			 var x = json1.img;
			 var idx = x.lastIndexOf("/");
			 if (idx > 0){
				 $("#file_name_c").val(x.substring(idx+20));
			 } 
		 }
		 if(json1.content){
			 $(".ddb").text(json1.content);
			 $("#c_wz_c").val(json1.content);
			 textareaLeftWords($('#c_wz_c')[0]);
		 }
		 if(json1.buttonState){
			 //1;//按钮为开启状态
			 if(json1.buttonState=='0'){
			 	$("#open2").attr("class",'kq kq1');
			 	$("#close2").attr("class",'gb gb1');
			 	$("#zhanqu1").hide();
			 	$("#zsq1").hide();
			 }else{
				 $("#zsq1").show();
				 $("#zhanqu1").show();
			 }
		 }
	 }else{
		 $("#zsq1").show();
		 $("#zhanqu1").show();
	 }
	 var displayAreaTwo = $("#displayAreaTwo").val();
	 if(displayAreaTwo &&displayAreaTwo!='{}'){
		 var json2 = jQuery.parseJSON(displayAreaTwo);
		 if(json2.chinese){
			 $("#name2").text(json2.chinese);
			 $("#d_wz_a").val(json2.chinese);
		 }
		 if(json2.english){
			 $(".zhy0629_titleh3").text(json2.english);
			 $("#d_wz_b").val(json2.english);
		 }
		 if(json2.img){
			 $("#ImgPr_d").attr("src",json2.img)
			 $("#show2_img").val(json2.img);
			 var x = json2.img;
			 var idx = x.lastIndexOf("/");
			 if (idx > 0){
				 $("#file_name_d").val(x.substring(idx+20));
			 } 
		 }
		 if(json2.content){
			 $(".pb").text(json2.content);
			 $("#d_wz_c").val(json2.content);
			 textareaLeftWords($('#d_wz_c')[0]);
		 }
		 if(json2.buttonState){
			 //1;//按钮为开启状态
			 if(json2.buttonState=='0'){
			 	$("#open3").attr("class",'kq kq1');
			 	$("#close3").attr("class",'gb gb1');
			 	$("#zhanqu2").hide();
			 	$("#zsq2").hide();
			 }else{
				 $("#zsq2").show();
				 $("#zhanqu2").show();
			 }
		 }
	 }else{
		 $("#zsq2").show();
		 $("#zhanqu2").show();
	 }
});
 
function save(){
	$("#create_domain_form").find(":text").blur(); 
	var browserTabImg = $("#browser_tab_img").val();
	var browserTabTitle = $("#title_rx").val();
	if(browserTabTitle){
		if($.trim(browserTabTitle).length > 20) {
			zhyAlert("请正确输入浏览器title文本！");
			return;
		}
	}
	
	var browserTabImg = $("#browser_tab_img").val();
	var logo = $("#logo").val();
	var bigImg = $("#big_img").val();
	var bizZhyCode = $("#bizZhyCode").val();
	var colorA = $(".dl_a").attr('class');
	var colorB = $(".zc_a").attr('class');
	var buttonState;
	if($("#open1").attr('class')=="kq"){
		buttonState=1;//按钮为开启状态
		var hotline = $("#hot_rx").val();
		if(hotline){
			var myreg = /^([0-9-])+$/;
			if(!myreg.test(hotline) || hotline.length<8 || hotline.length>15){
		    	zhyAlert('请输入正确的热线！');
		        return;
		    }
		}
	}else{
		buttonState=0;//按钮为关闭状态
	}
	//展区1
	var buttonStateOne;
	if($("#open2").attr('class')=="kq"){
		buttonStateOne=1;//展区1按钮为开启状态
		var show1Cname = $("#c_wz_a").val();
		if(show1Cname){
			var myreg = /^([0-9A-Za-z_-]|[\u4E00-\u9FA5]|[)(）（]|[\s])+$/;
			if(!myreg.test(show1Cname) || show1Cname.length<2 || show1Cname.length>20){
		    	zhyAlert('请输入正确的展示区 -1的中文标题！');
		        return;
		    }
		}
		var show1Ename = $("#c_wz_b").val();
		if(show1Ename){
			var myreg = /^([0-9A-Za-z_-]|[\u4E00-\u9FA5]|[)(）（]|[\s])+$/;
			if(!myreg.test(show1Ename) || show1Ename.length<2 || show1Ename.length>20){
		    	zhyAlert('请输入正确的展示区 -1的英文标题！');
		        return;
		    }
		}
		var show1Img = $("#show1_img").val();
		var show1Content = $("#c_wz_c").val();
	}else{
		buttonStateOne=0;//展区1按钮为关闭状态
	}
	//展区2
	var domainName =  $("#domainName").val();
	var domainState =  $("#domainState").val();
	var buttonStateTwo;
	if($("#open3").attr('class')=="kq"){
		buttonStateTwo=1;//展区2按钮为开启状态
		var show2Cname = $("#d_wz_a").val();
		if(show2Cname){
			var myreg = /^([0-9A-Za-z_-]|[\u4E00-\u9FA5]|[)(）（]|[\s])+$/;
			if(!myreg.test(show2Cname) || show2Cname.length<2 || show2Cname.length>20){
		    	zhyAlert('请输入正确的展示区 -2的中文标题！');
		        return;
		    }
		}
		var show2Ename = $("#d_wz_b").val();
		if(show2Ename){
			var myreg = /^([0-9A-Za-z_-]|[\u4E00-\u9FA5]|[)(）（]|[\s])+$/;
			if(!myreg.test(show2Ename) || show2Ename.length<2 || show2Ename.length>20){
		    	zhyAlert('请输入正确的展示区 -2的英文标题！');
		        return;
		    }
		}
		var show2Img = $("#show2_img").val();
		var show2Content = $("#d_wz_c").val();
	}else{
		buttonStateTwo=0;//展区2按钮为关闭状态
	}
	if($.trim(logo).length == 0) {
		zhyAlert("请选择logo图片！");
		return;
	}
	if($.trim(bigImg).length == 0) {
		zhyAlert("请选择logo下方的大图区图片！");
		return;
	}
	
	$.ajax({
		type:"POST",
		dataType:"json",
		url:"../customHome/createCustomHome",
		data:{
			browserTabImg:browserTabImg,
			browserTabTitle:browserTabTitle,
			logo:logo,
			bigImg:bigImg,
			hotline:hotline,
			buttonState:buttonState,
			colorA:colorA,
			colorB:colorB,
			bizZhyCode:bizZhyCode,
			show1Cname:show1Cname,
			show1Ename:show1Ename,
			show1Img:show1Img,
			show1Content:show1Content,
			buttonStateOne:buttonStateOne,
			show2Cname:show2Cname,
			show2Ename:show2Ename,
			show2Img:show2Img,
			show2Content:show2Content,
			buttonStateTwo:buttonStateTwo,
			domainName:domainName,
			domainState:domainState
		},
		async:false,
		success:function(data){
			if(data.data){
				window.location="../customHome/custom_home_ok";
			}else{
				zhyAlert(data.message);
			}
		}
	})
}

function preview(){
	$("#create_domain_form").find(":text").blur(); 
	var logo = $("#logo").val();
	$("#preview_logo").val(logo);
	var bigImg = $("#big_img").val();
	$("#preview_bigImg").val(bigImg);
	
	var browserTabImg = $("#browser_tab_img").val();
	$("#preview_browserImg").val(browserTabImg);
	var browserTabTitle = $("#title_rx").val();
	if(browserTabTitle){
		if($.trim(browserTabTitle).length > 20) {
			zhyAlert('请正确输入浏览器title文本！');
			return;
		}
	}
	$("#preview_browserTitle").val(browserTabTitle);
	
	var hotline = $("#hot_rx").val();
	if(hotline){
		var myreg = /^([0-9-])+$/;
		if(!myreg.test(hotline) || hotline.length<8 || hotline.length>15){
	    	zhyAlert('请输入正确的热线！');
	        return;
	    }else{
	    	$("#preview_hotline").val(hotline);
	    }
	}
	var buttonState;
	if($("#open1").attr('class')=="kq"){
		buttonState=1;//按钮为开启状态
	}else{
		buttonState=0;//按钮为关闭状态
	}
	$("#preview_buttonState").val(buttonState);
	
	 var colorA = $(".dl_a").attr('class');
	 $("#preview_colorA").val(colorA);
	 
	 var colorB = $(".zc_a").attr('class');
	 $("#preview_colorB").val(colorB);
	 
	var bizZhyCode = $("#bizZhyCode").val();
	$("#preview_bizZhyCode").val($("#bizZhyCode").val());
	//展区1
	var show1Cname = $("#c_wz_a").val();
	if(show1Cname){
		var myreg = /^([0-9A-Za-z_-]|[\u4E00-\u9FA5]|[)(）（]|[\s])+$/;
		if(!myreg.test(show1Cname) || show1Cname.length<2 || show1Cname.length>20){
	    	zhyAlert('请输入正确的展示区 -1的中文标题！');
	        return;
	    }else{
	    	$("#preview_show1Cname").val(show1Cname);
	    }
	}
	var show1Ename = $("#c_wz_b").val();
	if(show1Ename){
		var myreg = /^([0-9A-Za-z_-]|[\u4E00-\u9FA5]|[)(）（]|[\s])+$/;
		if(!myreg.test(show1Ename) || show1Ename.length<2 || show1Ename.length>20){
	    	zhyAlert('请输入正确的展示区 -1的英文标题！');
	        return;
	    }else{
	    	$("#preview_show1Ename").val(show1Ename);
	    }
	}
	var show1Img = $("#show1_img").val();
	$("#preview_show1Img").val(show1Img);
	
	var show1Content = $("#c_wz_c").val();
	$("#preview_show1Content").val(show1Content);
	
	
	if($("#open2").attr('class')=="kq"){
		buttonStateOne=1;//domainState区1按钮为开启状态
	}else{
		buttonStateOne=0;//展区1按钮为关闭状态
	}
	$("#preview_buttonStateOne").val(buttonStateOne);
	
	//展区2
	var show2Cname = $("#d_wz_a").val();
	if(show2Cname){
		var myreg = /^([0-9A-Za-z_-]|[\u4E00-\u9FA5]|[)(）（]|[\s])+$/;
		if(!myreg.test(show2Cname) || show2Cname.length<2 || show2Cname.length>20){
	    	zhyAlert('请输入正确的展示区 -2的中文标题！');
	        return;
	    }else{
    		$("#preview_show2Cname").val(show2Cname);
	    }
	}
	var show2Ename = $("#d_wz_b").val();
	if(show2Ename){
		var myreg = /^([0-9A-Za-z_-]|[\u4E00-\u9FA5]|[)(）（]|[\s])+$/;
		if(!myreg.test(show2Ename) || show2Ename.length<2 || show2Ename.length>20){
	    	zhyAlert('请输入正确的展示区 -2的英文标题！');
	        return;
	    }else{
    		$("#preview_show2Ename").val(show2Ename);
	    }
	}
	var show2Img = $("#show2_img").val();
	$("#preview_show2Img").val(show2Img);
	
	var show2Content = $("#d_wz_c").val();
	$("#preview_show2Content").val(show2Content);
	
	var buttonStateTwo;
	if($("#open3").attr('class')=="kq"){
		buttonStateTwo=1;//展区2按钮为开启状态
	}else{
		buttonStateTwo=0;//展区2按钮为关闭状态
	}
	$("#preview_buttonStateTwo").val(buttonStateTwo);
	
	var domainName =  $("#domainName").val();
	$("#preview_domainName").val(domainName);
	
	var domainState =  $("#domainState").val();
	$("#preview_domainState").val(domainState);
	
	if($.trim(logo).length == 0) {
		zhyAlert("请选择logo图片！");
		return;
	}
	if($.trim(bigImg).length == 0) {
		zhyAlert("请选择大图区图片！");
		return;
	} 
	
	document.getElementById("preview_home").submit();
}
function clear(){
	//清楚浏览器图片
	$("#file_name_e").val("");
	$("#img_up_e").val("");
	$("#browser_tab_img").val("");
	//清楚浏览器title
	$("#title_rx").val("");
	//清除logo相关
	$("#file_name_a").val("");
	$("#img_up_a").val("");
	$("#logo").val("");
	//清除大图
	$("#file_name_b").val("");
	$("#img_up_b").val("");
	$("#big_img").val("");
	//清除电话
	$("#hot_rx").val("");
	//清除展区1
	$("#c_wz_a").val("");
	$("#c_wz_b").val("");
	$("#file_name_c").val("");
	$("#img_up_c").val("");
	$("#show1_img").val("");
	$("#c_wz_c").val("");
	//清除展区2
	$("#d_wz_a").val("");
	$("#d_wz_b").val("");
	$("#file_name_d").val("");
	$("#img_up_d").val("");
	$("#show2_img").val("");
	$("#d_wz_c").val("");
}
////////////////////////////////图片上传////////////////////////////

$(function () {
	//上传浏览器图标
    upload("10","uploadBtne", function (data) {
    	$("#file_name_e").val(data.sourceNames[0])
        if(data.rs == -1){
            zhyAlert("请上传10K以内的jpg，gif，png图片格式！");
            return ;
        }
        $("#Imgicon").attr("src",data.paths[0]);
        $("#browser_tab_img").val(data.paths[0]);
    }, function (file, errorCode, message) {
        zhyAlert("请上传10K以内的jpg，gif，png图片格式！");
    });
    //上传logo
    upload("10","uploadBtna", function (data) {
    	$("#file_name_a").val(data.sourceNames[0])
        if(data.rs == -1){
        	zhyAlert("请上传10K以内的jpg，gif，png图片格式！");
            return ;
        }
        $("#ImgPr").attr("src",data.paths[0]);
        $("#logo").val(data.paths[0]);
    }, function (file, errorCode, message) {
    	zhyAlert("请上传10K以内的jpg，gif，png图片格式！");
    });
  //上传大图
    upload("4 MB","uploadBtnb", function (data) {
    	$("#file_name_b").val(data.sourceNames[0])
        if(data.rs == -1){
        	zhyAlert("请上传4M以内的jpg，gif，png图片格式！");
            return ;
        }
        $("#ImgPr_b").attr("src",data.paths[0]);
        $("#big_img").val(data.paths[0]);
    }, function (file, errorCode, message) {
    	zhyAlert("请上传4M以内的jpg，gif，png图片格式！");
    });
    //上传展示区一大图
    upload("4 MB","uploadBtnc", function (data) {
    	$("#file_name_c").val(data.sourceNames[0])
        if(data.rs == -1){
        	zhyAlert("请上传4M以内的jpg，gif，png图片格式！");
            return ;
        }
        $("#ImgPr_c").attr("src",data.paths[0]);
        $("#show1_img").val(data.paths[0]);
    }, function (file, errorCode, message) {
    	zhyAlert("请上传4M以内的jpg，gif，png图片格式！");
    });
    //上传展示区二大图
    upload("4 MB","uploadBtnd", function (data) {
    	$("#file_name_d").val(data.sourceNames[0])
        if(data.rs == -1){
        	zhyAlert("请上传4M以内的jpg，gif，png图片格式！");
            return ;
        }
        $("#ImgPr_d").attr("src",data.paths[0]);
        $("#show2_img").val(data.paths[0]);
    }, function (file, errorCode, message) {
    	zhyAlert("请上传4M以内的jpg，gif，png图片格式！");
    });
});
function upload(size,buttonId, successCallback, failCallback) {
    var bfUploadDefaultPoster;
    var settings = {
        flash_url: "https://res.sankai.com/st/klh_v1/swfupload/swfupload.swf",
        flash9_url: "https://res.sankai.com/st/klh_v1/swfupload_fp9.swf",
        upload_url: "https://res.sankai.com/uploadservice/resource/imgUpload.do",
        file_size_limit: size,
        file_types: "*.jpg;*.gif;*.png",
        file_post_name: "myFile",
        post_params: {"lineType": "klh"},
        file_types_description: "myFile",
        file_upload_limit: 10,
        file_queue_limit: 10,
        debug: false,
        // Button settings
        button_action: SWFUpload.BUTTON_ACTION.SELECT_FILE,
        button_image_url: "https://res.sankai.com/st/zhcloud_v1/img/zhy_pp_upbg.jpg",
        button_width: "102",
        button_height: "36",
        button_placeholder_id: buttonId,
        button_text: '',
        button_text_top_padding: 3,
        upload_success_handler: function (file, serverData) {
            var json = eval("(" + serverData + ")");
            successCallback(json);
        },
        file_dialog_complete_handler: function (numFilesSelected, numFilesQueued) {
            try {
                bfUploadDefaultPoster.startUpload()
            } catch (ex) {
                this.debug(ex);
            }
        },
        file_queue_error_handler: function (file, errorCode, message) {
            failCallback(file, errorCode, message);
        }
    };
    bfUploadDefaultPoster = new SWFUpload(settings);
}



